package com.bifrost.util;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * Author by yanxiaobo
 * Date on 2018/5/28$.
 */
public class DateTimeUtil {

    //joda-time

    //str->Date
    //Date->str
    public static final String STANDARD_FORMAT = "yyyy-MM-dd HH:mm:ss";




    public static Date strToDate(String dateTimeStr,String formatStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(formatStr);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date,String formatStr){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(formatStr);
    }

    public static Date strToDate(String dateTimeStr){
        DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern(STANDARD_FORMAT);
        DateTime dateTime = dateTimeFormatter.parseDateTime(dateTimeStr);
        return dateTime.toDate();
    }

    public static String dateToStr(Date date){
        if(date == null){
            return StringUtils.EMPTY;
        }
        DateTime dateTime = new DateTime(date);
        return dateTime.toString(STANDARD_FORMAT);
    }

    public static Date getsPecificDate(int dayOfMonth,int hourOfDay,int minuteOfHour,int secondOfMinute) {
        if(hourOfDay>=24 || minuteOfHour>60 || secondOfMinute>60)
            throw new IllegalArgumentException();
        DateTime dt=new DateTime().withDayOfMonth(dayOfMonth)
                .withHourOfDay(hourOfDay)
                .withMinuteOfHour(minuteOfHour)
                .withSecondOfMinute(secondOfMinute);
        return dt.toDate();
    }


    public static void main(String[] args) {
        //System.out.println(DateTimeUtil.dateToStr(new Date(),"yyyy-MM-dd HH:mm:ss"));
        //System.out.println(DateTimeUtil.strToDate("2010-01-01 11:11:11","yyyy-MM-dd HH:mm:ss"));
        //Date date = new Date();
        //String result = dateToStr(date7 06:30:00,"yyyyMMdd");
        //System.out.println(result);

        DateTime dt1=new DateTime().withDayOfMonth(7).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59);
        System.out.println(dt1.toDate());
        DateTime dt2=new DateTime().withDayOfMonth(7).withHourOfDay(6).withMinuteOfHour(30).withSecondOfMinute(0);
        System.out.println(dt2.toDate());
        System.out.println(dt2.toDate().compareTo(dt1.toDate()));




    }


}
